package com.basic.im.pay.controller.secondpay;


import com.basic.im.pay.controller.secondpay.utils.DateUtil;
import com.basic.im.pay.controller.secondpay.utils.HttpUtil;
import com.basic.im.pay.controller.secondpay.utils.SignUtil;
import com.basic.utils.StringUtil;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class SecondPay {
    public static void main(String[] args){

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("mchId", 20000094 + "");  //商户ID
        params.put("appId", "e23df2dde01b40289fdb1ca132c34876");  //商户应用ID
        params.put("productId", "8008");  //支付产品ID
        String orderno = StringUtil.getOutTradeNo();
        System.out.println(orderno);
        params.put("mchOrderNo", orderno);   //商户订单号
        params.put("currency", "cny");   //币种
        params.put("amount", "5000");   //支付金额
        params.put("clientIp", "210.73.10.148");   //客户端IP
        params.put("device", "ios10.3.1");   //客户端设备
        params.put("returnUrl", "http://localhost:8080/return_page.html");   //支付结果前端跳转URL
        params.put("notifyUrl", "http://43.199.8.90:8095/alipay/notify");   //支付结果后台回调URL
        params.put("subject", "网络购物");  //商品主题
        params.put("body", "网络购物");   //商品描述信息
        params.put("param1", ""+10044);   //扩展参数1
        params.put("param2", "");   //扩展参数2
        params.put("extra", "");  //附加参数
        params.put("reqTime", DateUtil.date2Str(new Date(), DateUtil.FORMAT_YYYYMMDDHHMMSS));  //请求时间, 格式yyyyMMddHHmmss
        params.put("version", "1.0");  //版本号, 固定参数1.0

        String sign = SignUtil.getSign(params, "1RWGEGISF4GGPDWF2WXDVJ4OCFZKFBKGVZBFQLSLHWKTMYHXN0FYPXZ8BM63KSJ8Z6H0QQIW5W1SSKFVBY64CTZFVEAAKCQUAWLZMHEFYEO7FFBR61L3XWTEJ4FWYS2X");  //签名
        params.put("sign", sign);

        try {
            System.out.println(HttpUtil.post("https://wg.kkzf.xyz" + "/api/pay/create_order", genUrlParams(params)));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * map 转换为  url参数
     * <p><b>Description: </b>
     * <p>2018年9月30日 上午10:12:33
     * @param paraMap
     * @return
     */
    public static String genUrlParams(Map<String, Object> paraMap) {
        if(paraMap == null || paraMap.isEmpty()) return "";
        StringBuffer urlParam = new StringBuffer();
        Set<String> keySet = paraMap.keySet();
        int i = 0;
        for(String key:keySet) {
            urlParam.append(key).append("=").append(paraMap.get(key));
            if(++i == keySet.size()) break;
            urlParam.append("&");
        }
        return urlParam.toString();
    }

}
